<template>
  <div class="login">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="avatar-box">
        <img src="~assets/image/logo.png" alt="">
      </div>
      <!-- 登陆表单区域 -->
      <el-form  ref="loginRef" label-width="0px" class="login-form" :model="loginForm" :rules="loginFormRules">
        <!-- 用户名区域 -->
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 密码区域 -->
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登陆</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { userLogin } from 'network/login'

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        // 验证用户名
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          { min: 3, max: 10, message: "用户名长度在3~10个字符", trigger: "blur" }
        ],
        // 验证密码
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          { min: 6, max: 15, message: "密码长度在6~15个字符", trigger: "blur" }
        ]
      }
    }
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginRef.resetFields() 
    },
    login() {
      this.$refs.loginRef.validate(async valid => {
        if(!valid) return;
        // const { data: res } = await this.$axios.post('login', this.loginForm)
        const res = await userLogin(this.loginForm)
        if(res.meta.status !== 200) 
          return this.$message.error("登陆失败! 请重新登陆");
        this.$message.success("登陆成功！");
        // 1. 将登陆成功之后的 token, 保存到客户端的 seesionStorage中
          // 1.1 项目中除了登陆之外的其他API接口, 必须登录后才能访问
          // 1.2 token 只应在当前网站打开时生效, 所以将token 保持在 seesionStorage 中
        window.sessionStorage.setItem('token', res.data.token);
        // 2. 通过编程式导航跳转到后台主页, 路由地址是 /home
        this.$router.push("/home")
      })
    }
  }
}
</script>

<style>
  .login {
    height: 100%;
    background-color: #2b5b8b;
  }

  .login-box {
    width: 480px;
    height: 300px;
    background-color: #fff;

    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .avatar-box {
    width: 100px;
    height: 100px;
    background-color: #fff;
    border-radius: 50%;
    border: 1px solid #eee;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;

    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .avatar-box img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }

  .login-form {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 0 20px;
  }

  .btns {
    display: flex;
    justify-content: flex-end;
  }
</style>